package com.sky.controller.common;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);

    //套餐分页查询
    @RequestMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询分类信息，参数为：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);

    }

    //新增分类
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类，参数为：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    //启用禁用
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用分类，状态为：{}，id为：{}", status, id);
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    //按类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
//        System.out.println("========================"+type);
//        log.info("根据类型查询分类，类型为：{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

    //修改分类
    @PutMapping
    public Result update(@RequestBody Category category) {
        log.info("修改分类，参数为：{}", category);
        categoryService.updateCategory(category);
        return Result.success();
    }

    //删除分类
    @DeleteMapping
    public Result deleteCategory(Long id) {
        log.info("删除分类，id为：{}", id);
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
